Ext.ns("DepartmentView");
var DepartmentView = function() {
	return this.setup();
};
DepartmentView.prototype.setup = function() {
	var f;
//	var l = this.initData();
	var l = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : __ctxPath + "/system/listDepUsers.do"
		}),
		reader : new Ext.data.JsonReader({
			root : "result",
			totalProperty : "totalCounts",
			fields : [ {
				name : "depUserId",
				type : "int"
			}, {
				name : "isMain",
				type : "int"
			}, {
				name : "sn",
				type : "int"
			}, "department", "staff" ]
		}),
		remoteSort : true
	});
	l.setDefaultSort("depUserId", "desc");
	l.load({
		params : {
			start : 0,
			limit : 25
		}
	});

	var e = new Ext.grid.CheckboxSelectionModel();
	var m = new Ext.grid.ColumnModel(
			{
				columns : [
						e,
						{
							header : "userId",
							dataIndex : "userId",
							hidden : true
						},
						{
							header : "",
							sortable : true,
							width : 5,
							dataIndex : "sn",
							renderer : function(s, p, o, t, r, q) {
								o.data["sn"] = t + 1;
								q.commitChanges();
								return o.data["sn"];
							}
						},
						/*{
							header : "状态",
							sortable : true,
							dataIndex : "appUser",
							width : 30,
							renderer : function(q) {
								if (q) {
									var o = q.status;
									var p = "";
									if (o == "1") {
										p += '<img title="激活" src="'
												+ __ctxPath
												+ '/resources/images/flag/customer/effective.png"/>';
									} else {
										p += '<img title="禁用" src="'
												+ __ctxPath
												+ '/resources/images/flag/customer/invalid.png"/>';
									}
									return p;
								}
							}
						},
						*/
						{
							header : "员工姓名",
							sortable : true,
							dataIndex : "staff",
							width : 60,
							renderer : function(o) {
								return o != null ? o.name : "";
							}
						},
						{
							header : "所属部门",
							sortable : true,
							dataIndex : "department",
							width : 60,
							renderer : function(o) {
								return o.depName == null ? "" : o.depName;
							}
						},
						{
							header : "主部门(是/否)",
							sortable : true,
							dataIndex : "isMain",
							width : 60,
							renderer : function(o) {
								return o == "1" ? "是" : "否";
							}
						},
						{
							header : "管理",
							dataIndex : "appUser",
							sortable : true,
							width : 100,
							renderer : function(x, t, p, s, v, u) {
								if (x) {
									var o = x.userId;
									var q = x.username;
									var w = p.data.department.depId;
									var r = "";
									if (isGranted("_AppUserDel") && o != 1) {
										r += '<button title="删除" value=" " class="btn-delete" onclick="DepartmentView.remove('
												+ p.data.depUserId
												+ ')"></button>';
									} else {
										r += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
									}
									if (isGranted("_AppUserEdit") && o != 1) {
										r += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="DepartmentView.edit('
												+ p.data.appUser.userId
												+ ",'"
												+ p.data.appUser.username
												+ "')\"></button>";
									} else {
										r += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
									}
									r += '&nbsp;<button title="查看部门信息" value=" " class="btn-showDetail" onclick="DepartmentView.show('
											+ p.data.appUser.userId
											+ ",'"
											+ p.data.appUser.username
											+ "')\"></button>";
									r += '&nbsp;<button title="添加上下级" value=" " class="btn-relativeJob" onclick="DepartmentView.addRelativeJob('
											+ o
											+ ",'"
											+ q
											+ "','"
											+ w
											+ "')\"></button>";
									return r;
								}
							}
						} ],
				defaults : {
					sortable : true,
					menuDisabled : true,
					width : 100
				},
				listeners : {
					hiddenchange : function(o, p, q) {
						saveConfig(p, q);
					}
				}
			});
	var c = new Ext.grid.GridPanel({
		region : "center",
		id : "DepUsersView",
		tbar : new Ext.Toolbar({
			defaultType : "button",
			items : [ {
				text : "添加",
				iconCls : "add-user",
				handler : function() {
					var f = Ext.getCmp("departmentTreePanel");
					var g = f.getSelectionModel().getSelectedNode();
					var h = 0;
					if (g != null) {
						h = g.id;
					}
					if (h == 0) {
						Ext.ux.Toast.msg(
								"操作信息",
								"请从左选择部门");
						return;
					}
					new DepUsersForm({
						depId : h,
						depName : g.text
					}).show();
				}
			}, "-", {
				text : "删除",
				iconCls : "btn-delete",
				handler : function() {
					DepartmentView.multiRemove();
				}
			} ]
		}),
		height : 800,
		title : "部门人员列表",
		store : l,
		shim : true,
		trackMouseOver : true,
		disableSelection : false,
		loadMask : true,
		cm : m,
		sm : e,
		viewConfig : {
			forceFit : true,
			enableRowBody : false,
			showPreview : false
		},
		bbar : new HT.PagingBar({
			store : l
		})
	});
//	c.addListener("rowdblclick", h);
	function h(p, o, q) {
		p.getSelectionModel().each(
				function(r) {
					DepartmentView.edit(r.data.appUser.userId,
							r.data.appUser.username);
				});
	}
//	l.load({
//		params : {
//			start : 0,
//			limit : 25
//		}
//	});
	var g = new Ext.tree.TreePanel({
		region : "west",
		id : "departmentTreePanel",
		title : "部门信息列表",
		collapsible : true,
		autoScroll : true,
		split : true,
		height : 800,
		width : 180,
		tbar : new Ext.Toolbar({
			items : [ {
				xtype : "button",
				iconCls : "btn-refresh",
				text : "刷新",
				handler : function() {
					g.root.reload();
				}
			}, "-", {
				xtype : "button",
				text : "展开",
				iconCls : "btn-expand",
				handler : function() {
					g.expandAll();
				}
			}, "-", {
				xtype : "button",
				text : "收起",
				iconCls : "btn-collapse",
				handler : function() {
					g.collapseAll();
				}
			} ]
		}),
		loader : new Ext.tree.TreeLoader({
			url : __ctxPath + "/system/getAllDepartment.do"
		}),
		root : new Ext.tree.AsyncTreeNode({
			expanded : true
		}),
		rootVisible : false,
		listeners : {
			"click" : DepartmentView.clickNode
		}
	});
	if (isGranted("_DepartmentAdd") || isGranted("_DepartmentEdit")
			|| isGranted("_DepartmentDel")) {
		g.on("contextmenu", b, g);
	}
	function b(o, p) {
		f = new Ext.tree.TreeNode({
			id : o.id,
			text : o.text
		});
		var q = new Ext.menu.Menu({
			items : []
		});
		q.clearMons();
		if (isGranted("_DepartmentAdd")) {
			q.add({
				text : "新建部门",
				iconCls : "btn-add",
				scope : this,
				handler : n
			});
		}
		if (isGranted("_DepartmentEdit")) {
			q.add({
				text : "修改部门信息",
				iconCls : "btn-edit",
				scope : this,
				handler : j
			});
		}
		if (isGranted("_DepartmentDel")) {
			q.add({
				text : "删除部门",
				iconCls : "btn-delete",
				scope : this,
				handler : k
			});
		}
		q.showAt(p.getXY());
	}
	function n() {
		var o = f.id;
		var p = Ext.getCmp("departmentForm");
		if (p == null) {
			if (o > 1) {
				new DepartmentForm({
					nodeId : o
				}).show();
			} else {
				new DepartmentForm({
					nodeId : 1
				}).show();
			}
			Ext.getCmp("departmentTreePanel").root.reload();
		}
	}
	function k() {
		var o = f.id;
		if (o > 1) {
			Ext.Msg.confirm("删除操作", "你确定删除部门?", function(p) {
				if (p == "yes") {
					Ext.Ajax.request({
						url : __ctxPath + "/system/deleteDepartment.do?depId="
								+ o,
						success : function(q, s) {
							var r = Ext.util.JSON.decode(q.responseText);
							if (r.success == false) {
								Ext.MessageBox.show({
									title : "操作提示",
									msg : r.msg,
									buttons : Ext.MessageBox.OK,
									icon : "ext-mb-error"
								});								
							} else {
								Ext.ux.Toast.msg("操作提示", "删除成功!");
							}
							Ext.getCmp("departmentTreePanel").root.reload();
						},
						failure : function(q, r) {
						    Ext.ux.Toast.msg("操作提示", "删除失败!");
						}
					});
				}
			});
		} 
	}
	function j() {
		var o = f.id;
		if (o > 1) {
			var p = Ext.getCmp("departmentForm");
			if (p == null) {
				new DepartmentForm().show();
				p = Ext.getCmp("departmentForm");
			}
				p.loadData({
				url : __ctxPath + "/system/getDepartment.do?department.depId="+o,
				preName : "department",
				root : "result",
				success : function(form, action) {
					
				} 
			
			});
		} 
	}
	var d = new Ext.FormPanel({
		id : "departmentViewSearchPanel",
		height : 40,
		frame : false,
		border : false,
		region : "north",
		layout : "form",
		layoutConfig : {
			padding : "5px",
			align : "middle"
		},
		items : [ {
			xtype : "container",
			layout : "column",
			border : false,
			fieldLabel : "请输入查询条件",
			style : "margin-top:8px;",
			defaults : {
				xtype : "label",
				border : false,
				height : 25
			},
			layoutConfig : {
				align : "middle"
			},
			items : [ {
				style : "margin:5px 5px 5px 5px",
				text : "员工姓名"
			}, {
				columnWidth : 0.2,
				xtype : "textfield",
				name : "Q_staff.name_S_LK",
				maxLength : 256
			}, {
				style : "margin: 5px 5px 5px 5px;",
				text : "主部门(是/否)"
			}, {
				columnWidth : 0.2,
				xtype : "combo",
				hiddenName : "Q_isMain_SN_EQ",
				displayField : "name",
				valueField : "id",
				mode : "local",
				triggerAction : "all",
				editable : false,
				store : [ [ "0", "否" ], [ "1", "是" ] ],
				emptyText : "全部"
			}, {
				style : "margin-left: 5px;",
				xtype : "button",
				text : "搜索",
				scope : this,
				iconCls : "search",
				handler : DepartmentView.search
			}, {
				xtype : "button",
				text : "清空",
				scope : this,
				iconCls : "reset",
				handler : DepartmentView.reset
			} ]
		} ]
	});
	
	
	//载入前带查询条件的翻页

	Ext.getCmp("DepUsersView").getStore().on('beforeload', function(store, options) {  
		var p = Ext.getCmp("departmentTreePanel").getSelectionModel().getSelectedNode();
		var e = Ext.getCmp("departmentViewSearchPanel");
		var b = e.getCmpByName("Q_staff.name_S_LK").getValue();
		var a = e.getCmpByName("Q_isMain_SN_EQ").getValue();      
	var new_params = {
		"Q_staff.name_S_LK" : b
	            }; 

	if (a == "0") {
		new_params["Q_isMain_SN_EQ"] = "0";
	} else if (a == "1") {
			new_params["Q_isMain_SN_EQ"] = "1";
		}

	if (p != null && p.id > 1) {
		new_params["Q_department.depId_L_EQ"] = p.id;
	}

	Ext.apply(options.params, new_params);  
	});

	
	
	var a = new Ext.Panel({
		id : "DepartmentView",
		title : "部门人员管理",
		closable : true,
		iconCls : "menu-department",
		layout : "border",
		items : [ g, d, c ],
		keys : [ {
			key : Ext.EventObject.ESC,
			fn : DepartmentView.reset,
			scope : this
		}, {
			key : Ext.EventObject.ENTER,
			fn : DepartmentView.search,
			scope : this
		} ]
	});
	return a;
};




//DepartmentView.prototype.initData = function() {
//	var a = new Ext.data.Store({
//		proxy : new Ext.data.HttpProxy({
//			url : __ctxPath + "/system/listDepUsers.do"
//		}),
//		reader : new Ext.data.JsonReader({
//			root : "result",
//			totalProperty : "totalCounts",
//			fields : [ {
//				name : "depUserId",
//				type : "int"
//			}, {
//				name : "isMain",
//				type : "int"
//			}, {
//				name : "sn",
//				type : "int"
//			}, "department", "appUser" ]
//		}),
//		remoteSort : true
//	});
//	a.setDefaultSort("depUserId", "desc");
//	return a;
//};
DepartmentView.multiRemove = function() {
	var b = Ext.getCmp("DepUsersView");
	var d = b.getSelectionModel().getSelections();
	if (d != null && d.length > 0) {
		var c = new Array();
		for ( var a = 0; a < d.length; a++) {
			c.push(d[a].data.depUserId);
		}
		DepartmentView.remove(c);
	} else {
		Ext.ux.Toast.msg("操作提示", "对不起，请选择你要删除的数据！");
	}
}, DepartmentView.remove = function(a) {
	Ext.Msg.confirm("删除操作", "你确定要删除该用户吗?", function(b) {
		if (b == "yes") {
			Ext.Ajax.request({
				url : __ctxPath + "/system/deleteDepUsers.do",
				method : "post",
				params : {
					ids : a
				},
				success : function(c) {
					Ext.ux.Toast.msg("操作提示", "用户删除成功");
					Ext.getCmp("DepUsersView").getStore().reload();
				},
				failure : function() {
					Ext.ux.Toast.msg("操作提示", "用户删除失败");
				}
			});
		}
	});
};
DepartmentView.clickNode = function(a) {
	DepartmentView.select(a);
};
DepartmentView.reset = function() {
	var a = Ext.getCmp("departmentViewSearchPanel");
	a.getForm().reset();
};
DepartmentView.search = function() {
	DepartmentView.select(null);
};
DepartmentView.select = function(f) {
	
	if(f){
		Ext.getCmp("departmentTreePanel").getSelectionModel().clearSelections();	
		Ext.getCmp("departmentTreePanel").getSelectionModel().select(f);}
//	var e = Ext.getCmp("departmentViewSearchPanel");
//	var h = e.getCmpByName("Q_appUser.username_S_LK").getValue();
//	var b = e.getCmpByName("Q_appUser.fullname_S_LK").getValue();
//	var a = e.getCmpByName("Q_isMain_SN_EQ").getValue();
	var g = Ext.getCmp("DepUsersView");
	var c = g.getStore();
	
	var d = {
		start : 0,
		limit : 25
//		"Q_appUser.username_S_LK" : h,
//		"Q_appUser.fullname_S_LK" : b
	};
//	if (a == "0") {
//		d["Q_isMain_SN_EQ"] = "";
//	} else {
//		if (a == "1") {
//			d["Q_isMain_SN_EQ"] = "1";
//		}
//	}
	
	c.reload({
		params : d
	});
};
//DepartmentView.add = function() {
//	var d = Ext.getCmp("departmentTreePanel").getSelectionModel()
//			.getSelectedNode();
//	var a = Ext.getCmp("centerTabPanel");
//	var b = Ext.getCmp("AppUserForm");
//	var e, c;
//	if (d != null && d.id > 0) {
//		e = d.id;
//		c = d.text;
//	}
//	if (b == null) {
//		b = new DepartmentForm("增加员工", null, e, c);
//		a.add(b);
//	} else {
//		a.remove(b);
//		b = new DepartmentForm("增加员工", null, e, c);
//		a.add(b);
//	}
//	a.activate(b);
//};
DepartmentView.edit = function(a, c) {
	var b = Ext.getCmp("departmentTreePanel").getSelectionModel()
			.getSelectedNode();
	AppUserView.edit(a, c);
};
DepartmentView.show = function(a, b) {
	DepUsersDetailForm.show(a, b);
};
DepartmentView.addRelativeJob = function(b, c, a) {
	new RelativeUserView({
		userId : b,
		username : c,
		depId : a
	}).show();
}, 
DepartmentView.sn = function(k, f) {
	var e = Ext.getCmp("DepUsersView");
	var m = e.getStore();
	var c = k;
	var d = k + f;
	var h = m.getAt(c);
	var l = m.getAt(d);
	var j = h.get("sn");
	var b = l.get("sn");
	h.data.sn = b;
	l.data.sn = j;
	var g = new Array();
	for (i = 0; i < m.getCount(); i++) {
		var a = m.getAt(i);
		g.push(a.data);
	}
	Ext.Ajax.request({
		url : __ctxPath + "/system/snDepUsers.do",
		method : "POST",
		success : function(n, o) {
			m.reload();
		},
		failure : function(n, o) {
		},
		params : {
			depParams : Ext.encode(g)
		}
	});
};